<?php defined('SYSPATH') or die('No direct script access.');

 /* $Id: class_group.php 55 2009-12-12 00:58:30Z Colonial Secretary @UndGerman$ */
 
class Friends_Core
{
// THIS METHOD RETURNS THE TOTAL NUMBER OF FRIENDS
	// INPUT: $direction (OPTIONAL) REPRESENTING A "0" FOR OUTGOING CONNECTIONS AND A "1" FOR INCOMING CONNECTIONS
	//	  $friend_status (OPTIONAL) REPRESENTING THE FRIEND STATUS (1 FOR CONFIRMED, 0 FOR PENDING REQUESTS)
	//	  $user_details (OPTIONAL) REPRESENTING WHETHER THE QUERY SHOULD JOIN TO THE USER TABLE OR NOT
	//	  $where (OPTIONAL) REPRESENTING ADDITIONAL THINGS TO INCLUDE IN THE WHERE CLAUSE
	// OUTPUT: AN INTEGER REPRESENTING THE NUMBER OF FRIENDS
  
	function user_friend_total($direction = 0, $friend_status = 1, $user_details = 0, $where = "")
  {
	  global $database, $setting;
    
    if( !$setting['setting_connection_allow'] )
      return 0;
    
    // BEGIN FRIEND QUERY
    $friend_query = "
      SELECT
        NULL
      FROM
        se_friends
    ";
    
    // JOIN TO FRIEND TABLE IF NECESSARY
    if( $user_details ) $friend_query .= "
      LEFT JOIN
        se_users
        ON ";
    
    if( $user_details && $direction==1 )
      $friend_query .= "se_friends.friend_user_id1=se_users.user_id ";
    elseif( $user_details )
      $friend_query .= "se_friends.friend_user_id2=se_users.user_id ";
    
    // CONTINUE QUERY
    $friend_query .= "
      WHERE
        friend_status='{$friend_status}'
    ";
    
    // EITHER "LIST OF WHO USER IS A FRIEND OF" OR "LIST OF USER'S FRIENDS"
    if( $direction == 1 ) $friend_query .= " &&
        friend_user_id2='{$this->user_info['user_id']}'
    ";
    
    if( $direction != 1 ) $friend_query .= " &&
        friend_user_id1='{$this->user_info['user_id']}'
    ";
    
    // ADD ADDITIONAL WHERE CLAUSE IF EXISTS
    if( $where ) $friend_query .= " &&
        {$where}
    ";
    
	  return (int) $database->database_num_rows($database->database_query($friend_query));
	}
  
  // END user_friend_total() METHOD








	// THIS METHOD RETURNS AN ARRAY OF USER'S FRIENDS
	// INPUT: $start REPRESENTING THE FRIEND TO START WITH
	//	  $limit REPRESENTING THE NUMBER OF FRIENDS TO RETURN
	//	  $direction (OPTIONAL) REPRESENTING A "0" FOR OUTGOING CONNECTIONS AND A "1" FOR INCOMING CONNECTIONS
	//	  $friend_status (OPTIONAL) REPRESENTING THE FRIEND STATUS (1 FOR CONFIRMED, 0 FOR PENDING REQUESTS)
	//	  $sort_by (OPTIONAL) REPRESENTING THE ORDER BY CLAUSE
	//	  $where (OPTIONAL) REPRESENTING ADDITIONAL THINGS TO INCLUDE IN THE WHERE CLAUSE
	//	  $friend_details (OPTIONAL) REPRESENTING A BOOLEAN THAT DETERMINES WHETHER OR NOT TO RETRIEVE THE "FRIEND TYPE" AND "FRIEND EXPLANATION"
	// OUTPUT: AN ARRAY OF THE USER'S FRIENDS
  
	function user_friend_list($start, $limit, $direction = 0, $friend_status = 1, $sort_by = "se_users.user_dateupdated DESC", $where = "", $friend_details = 0)
  {
	  global $database, $setting;
    
	  // SET VARIABLE
	  $friend_array = Array();
    
	  // MAKE SURE CONNECTIONS ARE ALLOWED
	  if( $setting['setting_connection_allow'] )
    {
	    // BEGIN FRIEND QUERY
	    $friend_query = "
        SELECT 
          se_friends.friend_id, 
          se_users.user_id, 
          se_users.user_username, 
          se_users.user_fname,
          se_users.user_lname,
          se_users.user_photo, 
          se_users.user_lastlogindate, 
          se_users.user_dateupdated,
          CASE
            WHEN (SELECT TRUE FROM se_friends WHERE friend_user_id1='{$this->user_info['user_id']}' AND friend_user_id2=se_users.user_id AND friend_status='1' LIMIT 1)
              THEN 2
            WHEN (SELECT TRUE FROM se_friends WHERE friend_user_id1='{$this->user_info['user_id']}' AND friend_user_id2=se_users.user_id AND friend_status='0' LIMIT 1)
              THEN 1
            ELSE 0
          END
          AS is_viewers_friend,
          CASE
            WHEN (SELECT TRUE FROM se_users AS se_users2 WHERE se_users2.user_id=se_users.user_id AND (user_blocklist LIKE '{$this->user_info['user_id']},%' OR user_blocklist LIKE '%,{$this->user_info['user_id']}' OR user_blocklist LIKE '%,{$this->user_info['user_id']},%') LIMIT 1)
              THEN TRUE
            ELSE FALSE
          END
          AS is_viewers_blocklisted
      ";
      
	    // GET FRIEND EXPLAIN, IF NECESSARY
	    if( $friend_details ) $friend_query .= ",
          se_friends.friend_type,
          se_friendexplains.friendexplain_body
      ";
      
	    // CONTINUE QUERY
	    $friend_query .= "
        FROM
          se_friends
        LEFT JOIN
          se_users ON
      ";
      
	    // MAKE SURE TO JOIN ON THE CORRECT FIELD (DEPENDENT ON DIRECTION)
	    if( $direction == 1 ) $friend_query .= "
        se_friends.friend_user_id1=se_users.user_id
      ";
      
	    if( $direction != 1 ) $friend_query .= "
        se_friends.friend_user_id2=se_users.user_id
      ";
      
	    // JOIN ON FRIEND EXPLAIN TABLE, IF NECESSARY
	    if( $friend_details ) $friend_query .= "
        LEFT JOIN
          se_friendexplains
          ON se_friends.friend_id=se_friendexplains.friendexplain_friend_id
      ";
      
	    // CONTINUE QUERY
	    $friend_query .= "
        WHERE
          friend_status='{$friend_status}'
      ";
      
	    // EITHER "LIST OF WHO USER IS A FRIEND OF" OR "LIST OF USER'S FRIENDS"
	    if( $direction == 1 ) $friend_query .= " &&
        friend_user_id2='{$this->user_info['user_id']}'
      ";
      
	    if( $direction != 1 ) $friend_query .= " &&
        friend_user_id1='{$this->user_info['user_id']}'
      ";
      
	    // ADD ADDITIONAL WHERE CLAUSE IF EXISTS
	    if( $where ) $friend_query .= " &&
          {$where}
      ";
      
	    // SET SORT	AND LIMIT
	    $friend_query .= "
        ORDER BY
          {$sort_by}
        LIMIT
          {$start}, {$limit}
      ";
      
	    // LOOP OVER FRIENDS
	    $friends = $database->database_query($friend_query);
	    while($friend_info = $database->database_fetch_assoc($friends))
      {
	      // CREATE AN OBJECT FOR FRIEND
	      $friend = new SEUser();
	      $friend->user_info['user_id'] = $friend_info['user_id'];
	      $friend->user_info['user_username'] = $friend_info['user_username'];
	      $friend->user_info['user_fname'] = $friend_info['user_fname'];
	      $friend->user_info['user_lname'] = $friend_info['user_lname'];
	      $friend->user_info['user_photo'] = $friend_info['user_photo'];
	      $friend->user_info['user_lastlogindate'] = $friend_info['user_lastlogindate'];
	      $friend->user_info['user_dateupdated'] = $friend_info['user_dateupdated'];
	      $friend->is_viewers_friend = $friend_info['is_viewers_friend'];
	      $friend->is_viewers_blocklist = $friend_info['is_viewers_blocklist'];
	      $friend->user_displayname();
        
	      // SET FRIEND TYPE/EXPLANATION VARS
	      if( $friend_details )
        {
	        $friend->friend_type = $friend_info['friend_type'];
	        $friend->friend_explain = $friend_info['friendexplain_body'];
	      }
        
	      // SET FRIEND ARRAY
	      $friend_array[] = $friend;
	    }
	  }
    
	  // RETURN FRIEND ARRAY
	  return $friend_array;
	}
  
  // END user_friend_list() METHOD








	// THIS METHOD ADDS A USER AS A FRIEND OF THE CURRENT USER
	// INPUT: $other_user_id REPRESENTING THE USER ID OF THE FRIEND TO BE ADDED
	//	  $friend_status REPRESENTING WHETHER THE FRIENDSHIP IS CONFIRMED OR NOT
	//	  $friend_type REPRESENTING A STRING WITH THE TYPE OF FRIEND
	//	  $friend_explain REPRESENTING A TEXTUAL EXPLANATION OF THE FRIENDSHIP
	// OUTPUT:
  
	function user_friend_add($other_user_id, $friend_status, $friend_type, $friend_explain)
  {
	  global $database;
    
	  // CHECK EXISTANCE OF FRIENDSHIP
	  if( $database->database_num_rows($database->database_query("SELECT TRUE FROM se_friends WHERE friend_user_id1='{$this->user_info['user_id']}' AND friend_user_id2='{$other_user_id}' LIMIT 1")) )
      return;

	  // ADD USER TO FRIENDS
	  $database->database_query("
      INSERT INTO se_friends
        (friend_user_id1, friend_user_id2, friend_status, friend_type)
      VALUES
        ('{$this->user_info['user_id']}', '{$other_user_id}', '{$friend_status}', '{$friend_type}'
      )
    ");
	  $friend_id = $database->database_insert_id();
    
	  $database->database_query("
      INSERT INTO se_friendexplains
        (friendexplain_friend_id, friendexplain_body)
      VALUES
        ('{$friend_id}', '{$friend_explain}')
    ");
    
	  // REMOVE FRIEND FROM BLOCKLIST
	  if( $this->user_blocked($other_user_id) )
    {
	    $blocklist = explode(",", $this->user_info['user_blocklist']);
	    $user_key = array_search($other_user_id, $blocklist);
      $blocklist[$user_key] = "";
	    $this->user_info['user_blocklist'] = implode(",", $blocklist);
	    $database->database_query("UPDATE se_users SET user_blocklist='{$this->user_info['user_blocklist']}' WHERE user_id='{$this->user_info['user_id']}' LIMIT 1");
	  }
	}
  
  // END user_friend_add() METHOD








	// THIS METHOD REMOVES A USER AS A FRIEND OF THE CURRENT USER
	// INPUT: $other_user_id REPRESENTING THE FRIEND'S USER ID
	// OUTPUT: 
  
	function user_friend_remove($other_user_id)
  {
	  global $database, $setting;
    
    // REMOVE IF FRIEND
    $friend1 = $database->database_query("SELECT friend_id FROM se_friends WHERE friend_user_id1='{$this->user_info['user_id']}' AND friend_user_id2='{$other_user_id}'");
    if( $database->database_num_rows($friend1) )
    {
      $friendship = $database->database_fetch_assoc($friend1);
      $database->database_query("DELETE FROM se_friends WHERE friend_id='{$friendship['friend_id']}' LIMIT 1");
      $database->database_query("DELETE FROM se_friendexplains WHERE friendexplain_friend_id='{$friendship['friend_id']}' LIMIT 1");
    }
    
    // REMOVE ADDITIONAL ROW IF TWO-DIRECTIONAL
    $friend2 = $database->database_query("SELECT friend_id FROM se_friends WHERE friend_user_id2='{$this->user_info['user_id']}' AND friend_user_id1='{$other_user_id}'");
    if( $database->database_num_rows($friend2) && ($setting['setting_connection_framework'] == 0 || $setting['setting_connection_framework'] == 2) )
    {
      $friendship = $database->database_fetch_assoc($friend2);
      $database->database_query("DELETE FROM se_friends WHERE friend_id='{$friendship['friend_id']}' LIMIT 1");
      $database->database_query("DELETE FROM se_friendexplains WHERE friendexplain_friend_id='{$friendship['friend_id']}' LIMIT 1");    
    }
	}
  
  // END user_friend_remove() METHOD








	// THIS METHOD RETURNS TRUE IF THE SPECIFIED USER IS A FRIEND OF A FRIEND OF THE EXISTING USER IN THIS CLASS
	// INPUT: $other_user_id REPRESENTING A USER'S USER ID
	// OUTPUT: RETURNS A BOOLEAN REPRESENTING WHETHER THE SPECIFIED USER IS A FRIEND OF A FRIEND OR NOT
  
	function user_friend_of_friend($other_user_id)
  {
	  global $database;
    
    $resource = $database->database_query("
      SELECT
        t2.friend_user_id2
      FROM
        se_friends AS t1
      LEFT JOIN
        se_friends AS t2
        ON t1.friend_user_id2=t2.friend_user_id1
      WHERE
        t1.friend_user_id1='{$this->user_info['user_id']}' &&
        t2.friend_user_id2='{$other_user_id}' &&
        t1.friend_status<>'0' &&
        t2.friend_status<>'0'
    ");
    
    return (bool) $database->database_num_rows($resource);
	}
  
  // END user_friend_of_friend() METHOD








	// THIS METHOD RETURNS TRUE IF THE SPECIFIED USER HAS BEEN FRIENDED BY THE EXISTING USER IN THIS CLASS
	// INPUT: $other_user_id REPRESENTING A USER'S USER ID
	//	  $friend_status (OPTIONAL) REPRESENTING WHETHER THE FRIENDSHIP IS CONFIRMED OR NOT
	// OUTPUT: RETURNS A BOOLEAN REPRESENTING WHETHER THE SPECIFIED USER IS FRIENDED OR NOT
  
	function user_friended($other_user_id, $friend_status = 1)
  {
	  global $database;
    
    $resource = $database->database_query("
      SELECT
        friend_id
      FROM
        se_friends
      WHERE
        friend_user_id1='{$this->user_info['user_id']}' &&
        friend_user_id2='{$other_user_id}' &&
        friend_status='{$friend_status}'
    ");
    
    return (bool) $database->database_num_rows($resource);
	}
  
  // END user_friended() METHOD








	// THIS METHOD RETURNS TRUE IF THE SPECIFIED USER HAS BEEN BLOCKED BY THE EXISTING USER IN THIS CLASS
	// INPUT: $other_user_id REPRESENTING A USER'S USER ID
	// OUTPUT: RETURNS A BOOLEAN REPRESENTING WHETHER THE SPECIFIED USER IS BLOCKED OR NOT
  
	function user_blocked($other_user_id)
  {
    if( !$this->level_info['level_profile_block'] || !$this->user_info['user_blocklist'] )
      return FALSE;
    
	  $blocklist = explode(",", $this->user_info['user_blocklist']);
    return in_array($other_user_id, $blocklist);
	}
  
  // END user_blocked() METHOD
?>